package com.song.rabbitmq.springbootorderrabbitmqproducerttl.service;

import com.song.rabbitmq.springbootorderrabbitmqproducerttl.callback.MessageConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    private String exchangeName = "ttl_order_exchange";
    private String routeKey = "";




    public void makeOrder1(Long userId, Long productId, int num) {
        String orderNumer = UUID.randomUUID().toString();
        String messge = "用户 " + userId + ",订单编号是：" + orderNumer;
        System.out.println(messge);
        rabbitTemplate.convertAndSend(exchangeName, routeKey, orderNumer);
    }


    /**
     * 模拟用户购买商品下单的业务
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderCallBackTest(Long userId, Long productId, int num) {
        // 1: 根据商品id查询库存是否充足
        // 2: 保存订单
        String orderNumer = UUID.randomUUID().toString();
        // 3: 发送消息

        //com.#  duanxin
        //#.email.* email
        //#.sms.# sms
        // 设置消息确认机制
        rabbitTemplate.setConfirmCallback(new MessageConfirmCallback());

        String messge = "用户 " + userId + ",订单编号是：" + orderNumer;
        System.out.println(messge);
        rabbitTemplate.convertAndSend(exchangeName, routeKey, orderNumer);
    }




}
